'use strict';
const db = uniCloud.database()
//修改指令
const dbCmd = db.command
exports.main = async (event, context) => {
	const {articleId,userId} = event
	
	const userInfo =await db.collection("user").doc(userId).get()
	const articleIds = userInfo.data[0].article_likes_ids
	let articleArr = null
	let msg=null
	//判断是否已经收藏了文章
	if(articleIds.includes(articleId)){
		//取消收藏
		articleArr = dbCmd.pull(articleId);
		msg = "取消收藏"
	}else{
		//添加收藏
		articleArr = dbCmd.addToSet(articleId)	;
		msg = "收藏成功"
	}
	//修改收藏列表
	await db.collection("user").doc(userId).update({
		article_likes_ids : articleArr
	})
	//重新获取用户信息
	const newUserInfo =await db.collection("user").doc(userId).get()
	//返回数据给客户端
	return {
		code:0,
		data:{
			msg,
			articleIds,//之前收藏列表
			newUserInfo:newUserInfo.data[0]//现在的用户信息
		}
	}
};
